UNPKG

generator-svalter

Version:

Generator to help you create and manage svelte/sapper application

29 lines (23 loc) 552 B
import posts from '../_posts.js'; const lookup = new Map(); posts.forEach(post => { lookup.set(post.slug, JSON.stringify(post)); }); export function get(req, res, next) { // the `slug` parameter is available because // this file is called index.json.js const { slug } = req.params; if (lookup.has(slug)) { res.writeHead(200, { 'Content-Type': 'application/json' }); res.end(lookup.get(slug)); } else { res.writeHead(404, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({ message: `Not found` })); } }